﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyKho.Server;

namespace QuanLyKho.Server.BLL
{
    public class TienNoBLL
    {
        public TienNoBLL()
        {
        }
        public List<TienNoDto> ListTienNo(DateTime? tuNgay, DateTime? denNgay)
        {
            List<TienNoDto> dmTNTempleClient = new List<TienNoDto>();
            TienNoDto objectTNClient = null;
            List<TienNo> dmTNTempleServer = new List<TienNo>();
            Dm_NhaPhanPhoiBLL dmNPPBll = new Dm_NhaPhanPhoiBLL();
            DateTime TuNgay = DateTime.Now;
            DateTime DenNgay = DateTime.Now;
            try
            {
                if(tuNgay != null)
                {
                    TuNgay = new DateTime(tuNgay.Value.Year, tuNgay.Value.Month, tuNgay.Value.Day, 0, 0, 0);
                    
                }
                if(denNgay != null)
                {
                    DenNgay = new DateTime(denNgay.Value.Year, denNgay.Value.Month, denNgay.Value.Day, 23, 59, 59);
                }
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    if(tuNgay == null && denNgay == null)
                    {
                        dmTNTempleServer = (from c in entities.TienNoes
                                      .Include("PhieuNhapThuoc")
                                  select c).ToList();
                    }
                    else if (tuNgay != null && denNgay == null)
                    {
                        dmTNTempleServer = (from c in entities.TienNoes
                                      .Include("PhieuNhapThuoc")
                                      where c.PhieuNhapThuoc.NgayNhap >= TuNgay
                                  select c).ToList();
                    }
                    else if (tuNgay == null && denNgay != null)
                    {
                        dmTNTempleServer = (from c in entities.TienNoes
                                      .Include("PhieuNhapThuoc")
                                            where c.PhieuNhapThuoc.NgayNhap <= DenNgay
                                            select c).ToList();
                    }
                    else
                    {
                        dmTNTempleServer = (from c in entities.TienNoes
                                      .Include("PhieuNhapThuoc")
                                            where c.PhieuNhapThuoc.NgayNhap >= TuNgay
                                               && c.PhieuNhapThuoc.NgayNhap <= DenNgay
                                            select c).ToList();
                    }
                }
                foreach (TienNo itemTN in dmTNTempleServer)
                {
                    objectTNClient = new TienNoDto();
                    objectTNClient.IdTienNo = itemTN.IdTienNo;
                    objectTNClient.IdPhieuNhap = itemTN.IdPhieuNhap;
                    objectTNClient.SoTien = itemTN.SoTien;
                    objectTNClient.LyDo = itemTN.LyDo;
                    objectTNClient.NgayTra = itemTN.NgayTra;
                    objectTNClient.PhieuNhapThuoc = itemTN.PhieuNhapThuoc;
                    objectTNClient.DmNhaPhanPhoi = dmNPPBll.FindObjectDmNhaPhanPhoi(itemTN.PhieuNhapThuoc.MaNhaPhanPhoi, "");
                    dmTNTempleClient.Add(objectTNClient);
                }

                dmTNTempleClient.OrderByDescending(u => u.IdPhieuNhap);
            }
            catch
            {
                dmTNTempleClient = null;
            }
            return dmTNTempleClient;
        }

        public List<TienNoDto> ListNhapNo(DateTime? tuNgay, DateTime? denNgay)
        {
            List<TienNoDto> dmTNTempleClient = new List<TienNoDto>();
            TienNoDto objectTNClient = null;
            List<PhieuNhapThuoc> dmTNTempleServer = new List<PhieuNhapThuoc>();
            Dm_NhaPhanPhoiBLL dmNPPBll = new Dm_NhaPhanPhoiBLL();
            DateTime TuNgay = DateTime.Now;
            DateTime DenNgay = DateTime.Now;
            try
            {
                if (tuNgay != null)
                {
                    TuNgay = new DateTime(tuNgay.Value.Year, tuNgay.Value.Month, tuNgay.Value.Day, 0, 0, 0);

                }
                if (denNgay != null)
                {
                    DenNgay = new DateTime(denNgay.Value.Year, denNgay.Value.Month, denNgay.Value.Day, 23, 59, 59);
                }
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    if (tuNgay == null && denNgay == null)
                    {
                        dmTNTempleServer = (from c in entities.PhieuNhapThuocs
                                            .Include("PhieuNhapThuocChiTiets")
                                            where c.LaPhieuTra == false
                                            && !entities.TienNoes.Any(idM => idM.IdPhieuNhap == c.IdPhieuNhap)
                                            select c).ToList();
                    }
                    else if (tuNgay != null && denNgay == null)
                    {
                        dmTNTempleServer = (from c in entities.PhieuNhapThuocs
                                            .Include("PhieuNhapThuocChiTiets")
                                            where c.NgayNhap >= TuNgay
                                            && c.LaPhieuTra == false
                                            && !entities.TienNoes.Any(idM => idM.IdPhieuNhap == c.IdPhieuNhap)
                                            select c).ToList();
                    }
                    else if (tuNgay == null && denNgay != null)
                    {
                        dmTNTempleServer = (from c in entities.PhieuNhapThuocs
                                            .Include("PhieuNhapThuocChiTiets")
                                            where c.NgayNhap <= DenNgay
                                            && c.LaPhieuTra == false
                                            && !entities.TienNoes.Any(idM => idM.IdPhieuNhap == c.IdPhieuNhap)
                                            select c).ToList();
                    }
                    else
                    {
                        dmTNTempleServer = (from c in entities.PhieuNhapThuocs
                                            .Include("PhieuNhapThuocChiTiets")
                                            where c.NgayNhap >= TuNgay
                                               && c.NgayNhap <= DenNgay
                                               && c.LaPhieuTra == false
                                               && !entities.TienNoes.Any(idM => idM.IdPhieuNhap == c.IdPhieuNhap)
                                            select c).ToList();
                    }
                }
                foreach (PhieuNhapThuoc itemTN in dmTNTempleServer)
                {
                    objectTNClient = new TienNoDto();
                    objectTNClient.IdTienNo = 0;
                    objectTNClient.IdPhieuNhap = itemTN.IdPhieuNhap;
                    objectTNClient.SoTien = itemTN.PhieuNhapThuocChiTiets.Sum(c=>c.SoLuong * c.GiaNhap);
                    objectTNClient.LyDo = "";
                    objectTNClient.NgayTra = null;
                    objectTNClient.PhieuNhapThuoc = itemTN;
                    objectTNClient.DmNhaPhanPhoi = dmNPPBll.FindObjectDmNhaPhanPhoi(itemTN.MaNhaPhanPhoi, "");
                    dmTNTempleClient.Add(objectTNClient);
                }
            }
            catch
            {
                dmTNTempleClient = null;
            }
            return dmTNTempleClient;
        }

        public List<PhieuNhapThuocChiTiet> ListPhieuNhapThuocChiTiet(int idPhieuNhap)
        {
            List<PhieuNhapThuocChiTiet> dmTNTempleClient = new List<PhieuNhapThuocChiTiet>();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmTNTempleClient = (from c in entities.PhieuNhapThuocChiTiets
                                  .Include("Dm_Thuoc")
                                        where c.IdPhieuNhap == idPhieuNhap
                                        select c).ToList();
                }
            }
            catch
            {
                dmTNTempleClient = null;
            }
            return dmTNTempleClient;
        }

        public string UpdateListTienNo(List<TienNoDto> listTienNoClient)
        {
            TienNo objectTienNoTemple = new TienNo();
            string error = "";
            int loi = -1;
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    foreach (TienNoDto itemTN in listTienNoClient)
                    {
                        objectTienNoTemple = entities.TienNoes.SingleOrDefault(c => c.IdTienNo == itemTN.IdTienNo);
                        objectTienNoTemple.NgayTra = itemTN.NgayTra;
                        loi = entities.SaveChanges();
                        if (loi < 0)
                        {
                            error = error + " * Lỗi xác nhận trả nợ cho: " + itemTN.DmNhaPhanPhoi.TenNhaPhanPhoi + "\n";
                        }
                        else
                        {
                            error = error + " - Xác nhận trả nợ cho: " + itemTN.DmNhaPhanPhoi.TenNhaPhanPhoi + " thành công\n";
                        }
                    }

                }
            }
            catch
            {
            }
            return error;
        }


        public List<TienNoDto> AddNewListTienNo(List<TienNoDto> listTienNoClient)
        {
            List<TienNoDto> dmDVTTemple = new List<TienNoDto>();
            TienNo objectlistTienNo = new TienNo();
            int error = -1;
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {

                    foreach (TienNoDto itemTienNo in listTienNoClient)
                    {
                        objectlistTienNo = new TienNo
                        {
                            SoTien = itemTienNo.SoTien,
                            LyDo = itemTienNo.LyDo,
                            IdPhieuNhap = itemTienNo.IdPhieuNhap
                        };
                        entities.TienNoes.Add(objectlistTienNo);
                        dmDVTTemple.Add(itemTienNo);
                    }
                    error = entities.SaveChanges();
                    if (error < 0)
                    {
                        dmDVTTemple = null;
                    }
                }
            }
            catch
            {
            }
            return dmDVTTemple;
        }

        public bool DeleteObjectTienNo(TienNoDto objiectTienNoClient)
        {
            TienNo objectTienNoTemple = new TienNo();
            int loi = -1;
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    objectTienNoTemple = entities.TienNoes.SingleOrDefault(c => c.IdTienNo == objiectTienNoClient.IdTienNo);
                    entities.TienNoes.Remove(objectTienNoTemple);
                    loi = entities.SaveChanges();
                }
            }
            catch
            {
            }
            return loi > 0 ? true : false;
        }

        public bool CreateTienNo(TienNo tienNo)
        {
            bool isSussess = false;
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                TienNo item = new TienNo();
                item.SoTien = tienNo.SoTien;
                item.IdPhieuNhap = tienNo.IdPhieuNhap;
                item.LyDo = tienNo.LyDo;
                entities.TienNoes.Add(item);
                entities.SaveChanges();
                isSussess = true;
            }

            return isSussess;
        }

        public bool UpdateTienNo(TienNo tienNo)
        {
            bool isSussess = false;
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                TienNo item = entities.TienNoes.FirstOrDefault(u => u.IdTienNo == tienNo.IdTienNo);
                item.SoTien = tienNo.SoTien;
                item.IdPhieuNhap = tienNo.IdPhieuNhap;
                item.LyDo = tienNo.LyDo;
                entities.SaveChanges();
                isSussess = true;
            }

            return isSussess;
        }

        public bool DeleteTienNo(int idTienNo)
        {
            bool isSussess = false;
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                TienNo item = entities.TienNoes.FirstOrDefault(u => u.IdTienNo == idTienNo);
                entities.TienNoes.Remove(item);
                entities.SaveChanges();
                isSussess = true;
            }

            return isSussess;
        }

        /// <summary>
        /// Thống kê tiền nợ
        /// </summary>
        /// <param name="tuNgay"></param>
        /// <param name="denNgay"></param>
        /// <param name="phuongPhap">0:đang nợ; 1:đã trả</param>
        /// <returns></returns>
        public List<TienNo> ThongKeTienNo(DateTime tuNgay, DateTime denNgay, int phuongPhap)
        {
            tuNgay = Convert.ToDateTime(tuNgay.ToShortDateString() + " 00:00:00");
            denNgay = Convert.ToDateTime(denNgay.ToShortDateString() + " 23:59:59");

            List<TienNo> listTienNo = new List<TienNo>();

            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                if (phuongPhap == 0)
                {
                    listTienNo = entities.TienNoes.Include("PhieuNhapThuoc")
                                                .Include("PhieuNhapThuoc.Dm_NhaPhanPhoi")
                                                .Where(u => u.PhieuNhapThuoc.NgayNhap >= tuNgay
                                                        && u.PhieuNhapThuoc.NgayNhap <= denNgay
                                                        && u.NgayTra == null)
                                                .ToList();
                }
                else
	            {
                    listTienNo = entities.TienNoes.Include("PhieuNhapThuoc")
                                                .Include("PhieuNhapThuoc.Dm_NhaPhanPhoi")
                                                .Where(u => u.NgayTra >= tuNgay
                                                        && u.NgayTra <= denNgay)
                                                .ToList();
	            }
            }

            return listTienNo;
        }
    }

    public partial class TienNoDto
    {
        public int IdTienNo { get; set; }
        public decimal SoTien { get; set; }
        public string LyDo { get; set; }
        public Nullable<int> IdPhieuNhap { get; set; }
        public Nullable<System.DateTime> NgayTra { get; set; }

        public virtual PhieuNhapThuoc PhieuNhapThuoc { get; set; }

        public virtual Dm_NhaPhanPhoi DmNhaPhanPhoi { get; set; }
    }
}
